package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.KichThuocDao;
import com.tt.fashionWebSite.dao.LoaiDanhMucDao;
import com.tt.fashionWebSite.domain.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_ProductSizeService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_productSizeService")
public class Ad_ProductSizeServiceImp extends GenericService implements Ad_ProductSizeService {
    @Autowired
    @Qualifier("kichThuoc")
    private KichThuocDao kichThuocDao;

    @Autowired
    @Qualifier("loaiDanhMuc")
    private LoaiDanhMucDao loaiDanhMucDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductSize(KichThuocDto productSizeDto) {
        KichThuoc kichThuoc = kichThuocDao.findById(productSizeDto.getMaSize());
        if(kichThuoc == null){
            kichThuoc = new KichThuoc();
            kichThuoc.setMaSize(productSizeDto.getMaSize());
            kichThuoc.setTenSize(productSizeDto.getTenSize());
            LoaiDanhMuc loaiDanhMuc = loaiDanhMucDao.findById(productSizeDto.getLoaiDanhMuc().getMaLoai());
            kichThuoc.setLoaiDanhMuc(loaiDanhMuc);
            kichThuocDao.insert(kichThuoc);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<LoaiDanhMucDto> getAllListCategoryCommon () {
        return mapper.mapCollection(loaiDanhMucDao.getAllProductCategory(), LoaiDanhMucDto.class);
    }

    public List<LoaiDanhMucDto> getAllListCategory () {
        return mapper.mapCollection(loaiDanhMucDao.getAllProductCategory(), LoaiDanhMucDto.class);
    }

    public List<KichThuocDto> getAllSize () {
        return mapper.mapCollection(kichThuocDao.getAllSize(), KichThuocDto.class);
    }

    public KichThuocDto getSizeInfoEdit(KichThuocDto sizeDto){
        KichThuoc kichThuoc = kichThuocDao.findById(sizeDto.getMaSize());
        KichThuocDto kichThuocDto = mapper.map(kichThuoc, KichThuocDto.class,KICH_THUOC_MAP_ID);
        return  kichThuocDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public KichThuocDto registerSizeEdit(KichThuocDto sizeDto) {
        KichThuoc kichThuoc = kichThuocDao.findById(sizeDto.getMaSize());
        try {
            kichThuoc.setMaSize(sizeDto.getMaSize());
            kichThuoc.setTenSize(sizeDto.getTenSize());
            LoaiDanhMuc loaiDanhMuc = loaiDanhMucDao.findById(sizeDto.getLoaiDanhMuc().getMaLoai());
            kichThuoc.setLoaiDanhMuc(loaiDanhMuc);
            kichThuocDao.update(kichThuoc);
            return sizeDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductSizeDelete(List<KichThuocDto> productSizeDto) {
        for (int i = 0; i < productSizeDto.size(); i++) {
            kichThuocDao.delete(productSizeDto.get(i).getMaSize());
        }
        return JsonResponseCode.SUCCESS;
    }
}
